/* ==================================================================   
 * Created 2013-10-28下午5:23:11 by 郑伟
 * ==================================================================  
 *  EMV  Level2
 * Copyright (c) SZZCS S&T Co.ltd ShenZhen, 2012-2013 
 * ================================================================== 
 * 深圳市中磁计算机技术有限公司拥有该文件的使用、复制、修改和分发的许可权
 * 如果你想得到更多信息，请访问 <http://www.szzcs.com/cn>
 *
 * SZZCS S&T Co.ltd ShenZhen owns permission to use, copy, modify and 
 * distribute this documentation.
 * For more information on EMV, please 
 * see <http://www.szzcs.com/cn>.  
 * ================================================================== 
 */
package com.hangup.activity;

import com.devil.bbsclient.R;
import com.hangup.net.ActivityKeys;
import com.hangup.net.HttpUtil;
import com.hangup.pojo.Post;
import com.hangup.util.JsonUtil;
import com.hangup.util.NetWorkUtil;
import com.hangup.util.PublicUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class TopicActivity extends Activity {
	private EditText themeET;
	private EditText textET;
	private Dialog dialog;
	private String url;
	private String postJson;
	private Post post_ = new Post();
	PublicApplication pa;
	private String result;

	Handler handler_ = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				Toast.makeText(TopicActivity.this, R.string.net,
						Toast.LENGTH_SHORT).show();
				break;
			case 1:
				dialog = NetWorkUtil.showDialog(TopicActivity.this, null, "发帖",
						"发帖中,请稍等");
				dialog.show();
				break;
			case 2:
				dialog.cancel();
				//
				pa.savePostDraft(pa.getStu().getStu_id(), themeET.getText()
						.toString(), textET.getText().toString());
				//
				Toast.makeText(TopicActivity.this, "连接服务器异常,已保存到草稿箱中!",
						Toast.LENGTH_SHORT).show();
				//
				finish();
				//
				break;
			case 3:
				dialog.cancel();
				Toast.makeText(TopicActivity.this, R.string.topic_failure,
						Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_topic);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		SysApplication.getInstance().addActivity(this);
		pa = (PublicApplication) getApplicationContext();

		themeET = (EditText) findViewById(R.id.topic_theme);
		textET = (EditText) findViewById(R.id.topic_text);
		if ((pa.getStu_id() != -1) && pa.getStu_id() == pa.getStu().getStu_id()) {
			themeET.setText(pa.getPost_theme());
			textET.setText(pa.getPost_text());
		}
	}

	public void cancel(View view) {
		finish();
	}

	public void publish(View view) {
		String theme = themeET.getText().toString();
		String text = textET.getText().toString();
		if (theme == null || theme.equals("")) {
			PublicUtil.setFocus(themeET);
			themeET.setError("请输入主题");
		} else if (text == null || text.equals("")) {
			PublicUtil.setFocus(textET);
			textET.setError("请输入内容");
		} else {
			if (NetWorkUtil.NetType(TopicActivity.this) == null) {
				handler_.sendMessage(handler_.obtainMessage(0));
				return;
			}
			handler_.sendMessage(handler_.obtainMessage(1));
			post_.setCount(0);
			post_.setStu_id(pa.getStu().getStu_id());
			post_.setTheme(theme);
			post_.setText(text);
			post_.setTime(PublicUtil.current());
			postJson = JsonUtil.format(post_);
			topic();

		}
	}

	protected void topic() {
		url = ActivityKeys.BASE_URL + ActivityKeys.ADD_POST_URL;
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!NetWorkUtil.webServer()) {
					handler_.sendMessage(handler_.obtainMessage(2));
					return;
				}
				// result = HttpUtil.getHttpPostResultForUrl(url);
				result = HttpUtil.postHttpResponseText(url, postJson);
				if (result == null) {// 发帖失败
					handler_.sendMessage(handler_.obtainMessage(3));
					return;
				} else if (result.equals(ActivityKeys.NET_FAILURE)) {
					handler_.sendMessage(handler_.obtainMessage(3));
				} else {
					Intent intent = new Intent();
					intent.setClass(TopicActivity.this, MainActivity.class);
					startActivity(intent);
				}
			}
		}).start();
	}
}
